﻿using System;
using System.Linq.Expressions;
using System.Web.Mvc;
using Htse.Blue.Helpers;
//using Microsoft.Web.Mvc.Internal;

namespace Htse.Blue.Logic.Validation
{
	public static class ValidationDictionaryExtension
	{
		/// <summary>
		/// Add an error to the validation dictionary for the specified model property showing the specified error message
		/// </summary>
		/// <typeparam name="TModel">Type of the model</typeparam>
		/// <typeparam name="TProperty">Type of the property</typeparam>
		/// <param name="validationDictionary"></param>
		/// <param name="expression">The model property expression</param>
		/// <param name="errorMessage">The error message to show</param>
		/// <returns></returns>
		public static IValidationDictionary<TModel> AddError<TModel, TProperty>(
			this IValidationDictionary<TModel> validationDictionary,
			Expression<Func<TModel, TProperty>> expression,
			string errorMessage) 
			where TModel : class
		{
			string inputName = ExpressionHelperExtension.GetInputName<TModel, TProperty>(expression); //ExpressionHelper.GetInputName<TModel, TProperty>(expression);

			validationDictionary.AddError(inputName, errorMessage);

			return validationDictionary;
		}

		/// <summary>
		/// Add an error to the validation dictionary for the specified pair of properties showing the specified error message
		/// </summary>
		/// <typeparam name="TModel">Type of the model</typeparam>
		/// <typeparam name="TProperty1">Type of the first property</typeparam>
		/// <typeparam name="TProperty2">Type of the second property</typeparam>
		/// <param name="validationDictionary"></param>
		/// <param name="expression1">The first model property expression</param>
		/// <param name="expression2">The second model property expression</param>
		/// <param name="errorMessage">The error message to show</param>
		/// <returns></returns>
		public static IValidationDictionary<TModel> AddError<TModel, TProperty1, TProperty2>(
			this IValidationDictionary<TModel> validationDictionary,
			Expression<Func<TModel, TProperty1>> expression1,
			Expression<Func<TModel, TProperty2>> expression2, 
			string errorMessage) 
			where TModel : class
		{
			string input1Name = ExpressionHelperExtension.GetInputName<TModel, TProperty1>(expression1); //ExpressionHelper.GetInputName<TModel, TProperty1>(expression1);
			string input2Name = ExpressionHelperExtension.GetInputName<TModel, TProperty2>(expression2); //ExpressionHelper.GetInputName<TModel, TProperty2>(expression2);

			validationDictionary.AddError(input1Name, input2Name, errorMessage);

			return validationDictionary;
		}
	}
}
